using Microsoft.EntityFrameworkCore;
using SheLife.Domain.Entities;
using SheLife.Domain.Interfaces;

namespace SheLife.Infrastructure.Repositories;

public class HabitRecordRepository : Repository<HabitRecord>, IHabitRecordRepository
{
    public HabitRecordRepository(ApplicationDbContext context) : base(context)
    {
    }

    public async Task<IEnumerable<HabitRecord>> GetByUserIdAsync(int userId)
    {
        return await _dbSet
            .Where(hr => hr.UserId == userId && !hr.IsDeleted)
            .OrderByDescending(hr => hr.RecordDate)
            .ToListAsync();
    }

    public async Task<IEnumerable<HabitRecord>> GetByHabitIdAsync(int habitId)
    {
        return await _dbSet
            .Where(hr => hr.HabitId == habitId && !hr.IsDeleted)
            .OrderByDescending(hr => hr.RecordDate)
            .ToListAsync();
    }

    public async Task<IEnumerable<HabitRecord>> GetByUserIdAndDateRangeAsync(int userId, DateTime startDate, DateTime endDate)
    {
        return await _dbSet
            .Where(hr => hr.UserId == userId && 
                        hr.RecordDate >= startDate && 
                        hr.RecordDate <= endDate && 
                        !hr.IsDeleted)
            .OrderByDescending(hr => hr.RecordDate)
            .ToListAsync();
    }

    public async Task<HabitRecord?> GetByUserIdAndHabitIdAndDateAsync(int userId, int habitId, DateTime date)
    {
        return await _dbSet
            .FirstOrDefaultAsync(hr => hr.UserId == userId && 
                                      hr.HabitId == habitId && 
                                      hr.RecordDate.Date == date.Date && 
                                      !hr.IsDeleted);
    }
}